package com.example.testquery;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.lenovo.nova.util.StreamUtil;
import com.lenovo.nova.util.StreamUtil.OnReadStreamHelper;
import com.lenovo.nova.util.StringUtil;
import com.lenovo.nova.util.debug.MyLog;
import com.lenovo.nova.util.debug.ToastAlertUtil;
import com.lenovo.nova.widget.BaseDialogFragment;
import com.scenic.afilechooser.utils.FileUtils;

public class UpdateDialog extends BaseDialogFragment implements OnClickListener {

	private static final String TAG = "UpdateDialog";
	Button mInstallFromSdCard;
	Button mInstallFromClient;
	Button mInstallFromNetwork;

	private Handler mHander = new Handler();
	private TextView mTxViewMsg = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onSetDefaultStyle() {
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_update, null);
		mInstallFromSdCard = (Button) view.findViewById(R.id.id_btn_install_sdcard);
		mInstallFromClient = (Button) view.findViewById(R.id.id_btn_install_network);
		mInstallFromNetwork = (Button) view.findViewById(R.id.id_btn_install_client);
		mTxViewMsg = (TextView) view.findViewById(R.id.id_txView_update_msg);

		mInstallFromSdCard.setOnClickListener(this);
		mInstallFromClient.setOnClickListener(this);
		mInstallFromNetwork.setOnClickListener(this);

		return view;
	}

	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getDialog().setTitle("设置选项");
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.id_btn_install_sdcard:
			ToastAlertUtil.alertToast(getActivity(), new Handler(), System.currentTimeMillis(), 
					"选择更新的源文件，默认是sdcard的根目录");
			showFileChooser();
			break;
		case R.id.id_btn_install_network:

			break;
		case R.id.id_btn_install_client:
			break;
		}
	}

	@SuppressLint("NewApi")
	private void showFileChooser() {
		Intent intent = new Intent(getActivity(),FileChooseActivity.class);
		startActivityForResult(intent, 0);
	}
	
	int fileNumberLine;
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(data != null){
			Uri uri = data.getData();
			final File file = FileUtils.getFile(getActivity(), uri);
			MyLog.p("onActivityResult " + requestCode + "  " + resultCode + " " + data + 
					"  " + file.getAbsolutePath());
			
			mTxViewMsg.setText("文件路径是：\n" + file.getAbsolutePath() + "\n " +
			"正在校验文件，请稍等。。。") ;
			
			final MainActivity_new context = (MainActivity_new) getActivity();
			final String filePath = file.getAbsolutePath();
			final String fileSuffix = StringUtil.getFileSuffix(filePath);
			
			new Thread(new Runnable() {
				@Override
				public void run() {
					if("xlsx".equals(fileSuffix)){
						final List<String> list = new ArrayList<String>();
//						String str = new XlsxReader(filePath).showAll(-1,new OnReadXlsxHelper() {
//							@Override
//							public void onReadLine(String arg0) {
//								list.add(arg0);
//							}
//						});
						
						fileNumberLine = list.size();
						mHander.postDelayed(new Runnable() {
							@Override
							public void run() {
								try {
									context.updateDatabase(fileNumberLine, list);
								} catch (Exception e) {
									e.printStackTrace();
								}
								dismiss();
							}
						}, 1800);
					}else{
						StreamUtil util = new StreamUtil();
						try {
							util.convertStreamToString(new FileInputStream(file), new OnReadStreamHelper() {
								@Override
								public void onReaderLine(String line) {
									fileNumberLine++;
								}
								
								@Override
								public boolean isFinish() {
									return !(isVisible() && !context.isFinishing());
								}
							});
						} catch (Exception e) {
							e.printStackTrace();
						}
						
						
						mHander.postDelayed(new Runnable() {
							@Override
							public void run() {
								try {
									context.updateDatabase(fileNumberLine, new FileInputStream(file));
								} catch (FileNotFoundException e) {
									e.printStackTrace();
								}
								dismiss();
							}
						}, 1800);
						
					}
					
					mHander.post(new Runnable() {
						@Override
						public void run() {
							mTxViewMsg.setText("检测到 " + 
						fileNumberLine + "行 即将更新数据库" );
						}
					});
				}
			}).start();
			
			
			
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
}
